package org.javaweb.cms_web.controller;

import org.javaweb.cms_web.model.User;
import org.javaweb.cms_web.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Date;

//账户注册
@Controller
public class AccountRegisterController {

    @Autowired
    private UserService userService; // 需要实现用户服务类

    @PostMapping("/submitRegistration")
    public String submitRegistration(
            @RequestParam("nickname") String nickname,
            @RequestParam("name") String name,
            @RequestParam("password") String password,
            @RequestParam("email") String email,
            @RequestParam("applicationReason") String applicationReason,
            @RequestParam("securityQuestion") String securityQuestion,
            @RequestParam("securityAnswer") String securityAnswer,
            @RequestParam("phoneNumber") String phoneNumber,
            Model model) {
        // 创建 User 对象
        User newUser = new User();
        newUser.setNickname(nickname);
        newUser.setName(name);
        newUser.setPassword(password);
        newUser.setEmail(email);
        newUser.setApplicationReason(applicationReason);
        newUser.setSecurityQuestion(securityQuestion);
        newUser.setSecurityAnswer(securityAnswer);
        newUser.setPhoneNumber(phoneNumber);
        newUser.setRegistrationTime(new Date());
        newUser.setStatus(0);

        // 将用户信息保存到数据库
        boolean success = userService.addUser(newUser); // addUser 为保存用户的服务方法

        if (success) {
            model.addAttribute("message", "注册申请已提交，等待管理员审核");
            return "registrationSuccess"; // 显示成功页面
        } else {
            model.addAttribute("message", "注册失败，请稍后再试");
            return "registrationError"; // 显示错误页面
        }
    }
}

